package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.code.petapp.R;
import com.code.petapp.bean.UserBean;
import com.code.petapp.databinding.ActivityAccountBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.UserUtils;

public class AccountActivity extends BaseActivity {
    private ActivityAccountBinding binding;
    private Double newAccount;
    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAccountBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Glide.with(this).load(R.drawable.icon)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(binding.imgAvatar);
        Intent intent = getIntent();
        double account = intent.getDoubleExtra("account", 0.00);
        binding.tvBalance.setText(String.format("%.2f", account));
        binding.money.setText("100");
        binding.btnCharge.setOnClickListener(v -> {
            String str=binding.money.getText().toString();
            if(!str.isEmpty()){
                double money = Double.parseDouble(str);
                newAccount=money+account;
                binding.btnCharge.setEnabled(false);
                load();
            }
        });
    }
    private void load(){
        UserBean userBean=new UserBean();
        userBean.setAccount(newAccount);
        userBean.setId(SPCommonUtils.getInstance().getUserId());
        UserUtils.update(SPCommonUtils.getInstance().getToken(), userBean, new HttpListener<>() {
            @Override
            public void success(String data) {
                toastShortShow("充值成功");
                finish();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
}